package vip.xiaonuo.megvii.captures.handle.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.megvii.captures.handle.ActivePushHandleImpl;
import vip.xiaonuo.megvii.captures.model.ShapeDistinguishEntity;
import vip.xiaonuo.megvii.captures.model.base.BindingInfo;
import vip.xiaonuo.modular.megviicapturedata.entity.MegviiCaptureData;
import vip.xiaonuo.modular.megviicapturedata.service.MegviiCaptureDataService;
import vip.xiaonuo.modular.megviipushdata.entity.MegviiPushData;
import vip.xiaonuo.modular.megviipushdata.service.MegviiPushDataService;
import vip.xiaonuo.modular.megviitrackidrelate.entity.MegviiTrackidRelate;
import vip.xiaonuo.modular.megviitrackidrelate.service.MegviiTrackidRelateService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class HumanHandlerService extends PushDataBaseHandler implements ActivePushHandleImpl<ShapeDistinguishEntity> {

    @Resource
    private MegviiCaptureDataService captureDataService;

    @Override
    public void handle(MegviiPushData data ) {
        try{
            String text = data.getValue();
            ShapeDistinguishEntity entity = JSON.parseObject(text,ShapeDistinguishEntity.class);

            // 业务数据转化与保存
            this.saveData(entity);

            // 保存原始数据处理结果
            data.setStatus(1);
            pushDataService.saveOrUpdate(data);
        }catch (Exception e){
            data.setStatus(2);
            data.setFailReason(ExceptionUtils.getMessage(e));
            pushDataService.saveOrUpdate(data);
        }
    }

    @Override
    public void saveData(ShapeDistinguishEntity entity) {
        List<BindingInfo> binds = entity.getBindingInfo();
        long trackId = entity.getTrack_id();

        // 保存关联关系表
        List<MegviiTrackidRelate> relates  = createRelates(trackId,binds);
        if(relates!=null && relates.size()>0){
            relateService.saveOrUpdateBatch(relates);
        }

        MegviiCaptureData captureData = new MegviiCaptureData();

        captureData.setTrackId(entity.getTrack_id());

        // 保存图片
        Long imgId  = this.saveImg(entity.getPhoto(),fileService);
        captureData.setImgId(imgId);
        Long fullImgId = this.saveImg(entity.getFullImage(),fileService);
        captureData.setFullImgId(fullImgId);

        captureData.setDeviceId(entity.getDevice_id());
        captureData.setChannelName(entity.getScreen_token());
        captureData.setChannelNo(entity.getVideo_number());
        captureData.setObjType(entity.convert(entity.getObjType()));
        captureData.setGender(entity.getHumanAttr().getGender());

        captureData.setClothes(entity.getHumanAttr().getClothes().getClothesDesc());
        captureData.setPants(entity.getHumanAttr().getPants().getPantsDesc());

        captureData.setBags(entity.getHumanAttr().getBags().getBagCarry());
        captureData.setSafetyhelmet(entity.getHumanAttr().getSafetyHelmet().getSafetyHelmetWear());

        captureData.setCaptureTime(new Date(entity.getTimestamp()));

        // TODO 补充其他信息
        captureDataService.save(captureData);
    }


}
